package com.zjq.smartdevice.controller;

import com.zjq.smartdevice.dto.R;
import com.zjq.smartdevice.pojo.SysDevice;
import com.zjq.smartdevice.service.SysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @User zhangjiaqi
 * @Project project
 * @Package com.zjq.smartdevice.controller
 * @Date 2022/3/23 21:59
 */
@RestController
    @RequestMapping("/devices")
public class SysDeviceController {

    private SysDeviceService sysDeviceService;

    @Autowired
    public void setSysDeviceService(SysDeviceService sysDeviceService) {
        this.sysDeviceService = sysDeviceService;
    }

    @PostMapping("/device")
    public R addSysDevice(@RequestBody SysDevice sysDevice) {
        sysDeviceService.addSysDevice(sysDevice);
        return R.setOK();
    }

    @GetMapping("/device/{deviceId}")
    public R findById(@PathVariable String deviceId) {
        SysDevice sysDevice = sysDeviceService.findById(deviceId);
        return R.setOK(sysDevice);
    }

    @RequestMapping("/devices")
    public R findAll(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "1")  int limit, SysDevice device) {
        return R.setOK(sysDeviceService.findByPage(page, limit, device));
    }

    @PostMapping("/command/{devideId}/{command}")
    public R sendControl(@PathVariable String deviceId, @PathVariable String command) {
        System.err.println("收到了向设备发送的命令的操作" + deviceId + "---->" + command);
        sysDeviceService.sendControl(deviceId, command);
        return R.setOK();
    }



}
